<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2016/11/23
 * Time: 17:02
 */

namespace Home\Controller;


use Home\Biz\FieldBiz;
use Think\Controller;

class ProcessController extends LoginBaseController
{


    /**
     *
     */
    public function processList()
    {
        $this->assign('group_name', FieldBiz::getGroup());
        $this->display('process/productlist');
    }


    public function processView()
    {

        $product = FieldBiz::loadDetail(I('product_id'));
        $process_flag = $product[0]['process_flag'];

        //判断是否处理过
        $ko_group = C('PROCESS_LINE.ko_group') & $process_flag;
        $cp_group = C('PROCESS_LINE.cp_group') & $process_flag;
        $soft_group = C('PROCESS_LINE.soft_group') & $process_flag;
        $apply_group = C('PROCESS_LINE.apply_group') & $process_flag;
        $this->assign('ko_group', $ko_group == 0 ? false : true);
        $this->assign('cp_group', $cp_group == 0 ? false : true);
        $this->assign('soft_group', $soft_group == 0 ? false : true);
        $this->assign('apply_group', $apply_group == 0 ? false : true);

        $this->assign('server_name', "http://" . $_SERVER["SERVER_NAME"]);
        $this->assign('group_name', FieldBiz::getGroup());

        $this->assign('product', $product[0]);
        $this->display("process/productline-info");
    }

    public function diff()
    {
        $a = array(
            'a' => '44',
            'b' => '44',
            'c' => '44',
            'd' => '44'
        );
        $b = array(
            'a' => '66',
            'c' => '88',
            'd' => '44'
        );

        print_r(array_diff_assoc($b, $a));
    }

    /**
     * 编辑产品
     */
    public function editProduct()
    {
        $this->assign('id', I('product_id'));
        $detail = FieldBiz::loadDetail(I('product_id'));
        $process_flag = $detail[0]['process_flag'];

        //判断是否处理过
        $ko_group = C('PROCESS_LINE.ko_group') & $process_flag;
        $cp_group = C('PROCESS_LINE.cp_group') & $process_flag;
        $soft_group = C('PROCESS_LINE.soft_group') & $process_flag;
        $apply_group = C('PROCESS_LINE.apply_group') & $process_flag;
        $this->assign('ko_group', $ko_group == 0 ? false : true);
        $this->assign('cp_group', $cp_group == 0 ? false : true);
        $this->assign('soft_group', $soft_group == 0 ? false : true);
        $this->assign('apply_group', $apply_group == 0 ? false : true);

        $this->assign('server_name', "http://" . $_SERVER["SERVER_NAME"]);
        $this->assign('group_name', FieldBiz::getGroup());
        $this->display('process/editproduct');
    }

    /**
     * 上传
     */
    public function upload()
    {
        $upload_config = array(
            'maxSize' => 0,
            'rootPath' => './Public/Uploads/',
            'savePath' => '',
            'saveName' => uniqid(),
            'exts' => array('jpg', 'png', 'pdf'),
            'subName' => '',
            'replace' => true,
        );

        ini_set('max_execution_time', '0');
        $upload = new \Think\Upload($upload_config); // 实例化上传类
        $upload->autoSub = false;

        $info = $upload->upload();

        $this->ajaxReturn(array(path => "/Public/Uploads/" . $info['file']['savepath'] . $info['file']['savename']));
    }

    /**
     * 下载
     */
    //下载文件
    function download()
    {
        $file = I('path');
        $name = I('filename');


        $file_sub_path = $_SERVER['DOCUMENT_ROOT'];
        $file_path = $file_sub_path . $file;

        $name = $name.".".pathinfo($file_path, PATHINFO_EXTENSION);
        $file_name = iconv("utf-8", "utf-8", $name);
        $fp = fopen($file_path, "r");
        $file_size = filesize($file_path);
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:" . $file_size);
        Header("Content-Disposition: attachment; filename=" . $file_name);
        $buffer = 1024;
        $file_count = 0;
        while (!feof($fp) && $file_count < $file_size) {
            $file_con = fread($fp, $buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
        exit();

    }
}